package models;

import java.net.URL;
import java.util.*;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import play.data.validation.Required;
import play.db.jpa.Model;
import play.db.jpa.Blob;
import play.modules.search.Field;
import play.modules.search.Indexed;
import service.CreditManager;

@Entity
@Indexed
public class User extends Model
{
	
	//Discarding these parameters cause hibernate error,
	//These columns should manually removed from user table on database
	//This parameters are stored in CreditManager.java
	//No need to store these static variables in every record
    
	public double INITIAL_BALANCE = CreditManager.INITIAL_BALANCE;
    public int INITIAL_REPUTATION = CreditManager.INITIAL_REPUTATION;

     @Required
     public String email;

     @Required
     public String password;

     // @Required
     // public String nickname;

     @Required
     @Field
     public String fullname;

     @Required
     public Boolean isAdmin;
     
     
     public Double userRating=3.0;

     @Required
     @Temporal(TemporalType.TIMESTAMP)
     public Date registrationDate;

     @Deprecated
     public String address;

     // @Required
     // public String phone;

     // @OneToOne
     // public UploadedFile avatar;

     @Required
     public Double balance;

     @Required
     public Integer reputation;
     
 	 @ManyToMany(cascade=CascadeType.ALL)
     public List<Tag> interests = new ArrayList<Tag>();
     //@Required
     // public URL photolink;

     // @Required
     // @Temporal(TemporalType.DATE)
     // public Date birthday;

     // @Required
     // public String job;

     // @Required
     // public City city;

     // @Required
     // public County county;

     // @Required
     // @Enumerated(EnumType.STRING)
     // public Status status;
     
     
     @Enumerated(EnumType.STRING)
     public BadgeType badge;
     
     @OneToMany(mappedBy="user")
     public List<Comment> comments;
     
     @OneToMany(mappedBy="receiver")
     public List<PrivateMessage> inbox;
     
     @OneToMany(mappedBy="sender")
     public List<PrivateMessage> outbox;

     /**
      * Services to which the user has applied.
      */
     @OneToMany(mappedBy="user", cascade={CascadeType.ALL})
     public Set<ServiceHasParticipants> appliedServices = new HashSet();

     public static List<User> getNewUsers(int maxUsers) {
	 return find("order by registrationDate DESC").fetch(maxUsers);
     }
     
     public static User connect(String email, String password) {
	 return find("byEmailAndPassword", email, password).first();
     }

     public User() {
	 this.registrationDate = new Date();
	 this.isAdmin = false;
	 this.balance = CreditManager.INITIAL_BALANCE;
	 this.reputation = CreditManager.INITIAL_REPUTATION;
     }	 

     public Blob photo;     
     
     public Set<Service> getServices() {
 		Set<Service> services = new HashSet<Service>();
 		
 		for (ServiceHasParticipants relation : this.appliedServices) {
 			services.add(relation.service);
 		}
 		
 		return services;
 	}
}

